package com.example.springtest.teacher.service.impl;


import com.example.springtest.common.ServiceCode;
import com.example.springtest.common.ServiceException;
import com.example.springtest.teacher.mapper.TeacherMapper;
import com.example.springtest.teacher.pojo.Teacher;
import com.example.springtest.teacher.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;


    @Override
    public List<Teacher> getTeacherList(){
        List<Teacher> teacherList=teacherMapper.getList();
        return  teacherList;
    }
    @Override
    public void addTeacher(Teacher teacher){
        int result=teacherMapper.add(teacher);
        if(result!=1){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"增加教师失败！");
        }
    }
    @Override
    public void deleteTeacher(Integer id){
        int result=teacherMapper.delete(id);
        if(result!=1){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"删除教师失败！");
        }

    }

    public void editTeacher(Integer id,Teacher teacher){
        Teacher teacher1=teacherMapper.getById(id);
        teacher1.setName(teacher.getName());
        teacher1.setAge(teacher.getAge());
        teacher1.setMajor(teacher.getMajor());
        teacher1.setAddress(teacher.getAddress());
        int result=teacherMapper.edit(teacher1);
        if(result!=1){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"编辑教师失败！");
        }
    }
}
